package com.example.admin.controller;

import com.example.admin.model.LoginLog;
import com.example.admin.model.OperationLog;
import com.example.admin.service.LogService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/logs")
public class LogController {
    private final LogService logService;

    public LogController(LogService logService) {
        this.logService = logService;
    }

    @GetMapping("/operations")
    @PreAuthorize("hasRole('ADMIN')")
    public Page<OperationLog> getOperationLogs(
            @RequestParam(required = false) Long userId,
            @PageableDefault Pageable pageable) {
        return logService.getOperationLogs(userId, pageable);
    }

    @GetMapping("/logins")
    @PreAuthorize("hasRole('ADMIN')")
    public Page<LoginLog> getLoginLogs(
            @RequestParam(required = false) Long userId,
            @PageableDefault Pageable pageable) {
        return logService.getLoginLogs(userId, pageable);
    }
}
